package com.bosch.in.jfree.chart;

import java.util.ArrayList;
import java.util.List;
import org.jfree.chart.imagemap.ImageMapUtilities;
import org.jfree.chart.imagemap.ToolTipTagFragmentGenerator;

public class DefaultToolTipTagFragmentGenerator implements ToolTipTagFragmentGenerator{
	private int index;
	private List<String> toolTips;
	private boolean isClickableToolTip = false;
	private String captionTemplate;
	
	/**
	 * Default constructor
	 */
	public DefaultToolTipTagFragmentGenerator()
	{
		this.toolTips = new ArrayList<String>();
	}
	
	/**
	 * 
	 * @param toolTips
	 */
	public DefaultToolTipTagFragmentGenerator(List<String> toolTips)
	{
		this.toolTips = toolTips;
		this.initialize();
	}
	
	/**
	 * 
	 * @param toolTips
	 */
	public DefaultToolTipTagFragmentGenerator(List<String> toolTips,String captionTemplate)
	{
		this.toolTips = toolTips;
		this.initialize();
		this.captionTemplate = captionTemplate;
	}
	
	/**
	 * @param toolTips
	 * @param isClickableTooltip
	 */
	public DefaultToolTipTagFragmentGenerator(List<String> toolTips,boolean isClickableTooltip)
	{
		this.toolTips = toolTips;
		this.isClickableToolTip = isClickableTooltip;
		this.initialize();
	}
	
	@Override
	public String generateToolTipFragment(String toolTipText) {
			return this.generateToolTip(toolTipText);
	}
	
	/**
	 * Initialization
	 */
	private void initialize()
	{
		this.index = toolTips.size();
		this.captionTemplate = "<span style='white-space:nowrap;'> {1} </span>" + "<br/>" + 
		  "<span style='white-space:nowrap;'>{2}</span>";
	}
	
	/**
	 * 
	 * @param toolTipText
	 * @return
	 */
	private String generateToolTip(String toolTipText)
	{
		int i = (index > 0) ? --index : index;
		while (toolTips.get(index)==null&&index>0)
		{
				i=--index;
		}
		return getTooltip(toolTipText,toolTips.get(i));
	}
	
	/**
	 * Build ToolTip string from parameters
	 * @param capt
	 * @param info
	 * @return
	 */
	private String getTooltip(String capt, String info)
	{
		if (isClickableToolTip)
		{
			return getClickableTooltip(capt, info);
		}
		return getNormalTooltip(capt, info);
	}
	
	/**
	 * 
	 * @param capt
	 * @param info
	 * @return
	 */
	protected String buildCaption(String capt, String info)
	{
		return captionTemplate.replace("{1}", capt).replace("{2}", info);
	}
	
	/**
	 * Build clickable ToolTip string from parameters
	 * @param capt
	 * @param info
	 * @return
	 */
	private String getClickableTooltip(String capt, String info) {
		return getNormalTooltip(capt,info)+" onClick=\"return chartPointClicked(this,'"+capt+"','"+ImageMapUtilities.javascriptEscape(this.buildCaption(capt, info))+"');\"";
	}
	
	/**
	 * Build normal ToolTip string from parameters
	 * @param capt
	 * @param info
	 * @return
	 */
	private String getNormalTooltip(String capt, String info) {
			return " onMouseOver=\"return ShowContent(['"
			+ ImageMapUtilities.javascriptEscape(this.buildCaption(capt, info))+ "']);\"" + " onMouseOut=\"return HideContent();\"";
	}
	
}
